package whu.o2o;

import whu.o2o.util.Utils;
import whu.o2o.vo.CommentVO;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;

import whu.o2o.R;



public class DiaryBaseActivity extends BaseActivity implements OnClickListener, TextWatcher, OnTouchListener {
	private RelativeLayout mCommentLlyt;
	private Button mSendComment;
	private EditText mCommentContent;

	/**
	 * ��ʾ���������
	 */
	public void showCommentBox(CommentVO commentVO) {
		if (mCommentLlyt == null) {
			initCommentBox();
		}

		mCommentLlyt.setVisibility(View.VISIBLE);
		mSendComment.setTag(commentVO);
		mCommentContent.requestFocus();
		Utils.showIme(this, mCommentContent);
	}

	private void hideCommentBox() {
		Utils.hideIme(this);
		mCommentLlyt.setVisibility(View.GONE);
	}

	private void initCommentBox() {
		mCommentLlyt = (RelativeLayout) findViewById(R.id.comment_rlyt);
		mSendComment = (Button) findViewById(R.id.send);
		mCommentContent = (EditText) findViewById(R.id.comment_content);
		ListView listView = (ListView) findViewById(R.id.listView);

		listView.setOnTouchListener(this);
		mSendComment.setOnClickListener(this);
		mCommentContent.addTextChangedListener(this);
	}

	@Override
	public void afterTextChanged(Editable arg0) {
		if (TextUtils.isEmpty(mCommentContent.getText().toString().trim())) {
			mSendComment.setEnabled(false);
		} else {
			mSendComment.setEnabled(true);
		}
	}

	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

	}

	@Override
	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

	}

	@Override
	public void onClick(View v) {
		Intent intent;

		switch (v.getId()) {
		case R.id.write:
			
			break;

		case R.id.search:

			break;
		}

	}

	@Override
	public boolean onTouch(View arg0, MotionEvent arg1) {
		hideCommentBox();
		return false;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0 && mCommentLlyt != null
				&& mCommentLlyt.getVisibility() == View.VISIBLE) {
			hideCommentBox();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
